package capstoneproject.dudiachi.Main;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import android.content.Intent;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.Toast;
import capstoneproject.dudiachi.Controller.OldDistrictManager;
import capstoneproject.dudiachi.Model.OldDistrict;
import capstoneproject.dudiachi.Model.OldProvince;

public class OldDistrictActivity extends BaseActivity {

	private ArrayList<Bitmap> bitArray;
	private ListView oldDistrictView;
	private OldDistrictListAdapter adapter;
	private ArrayList<OldDistrict> oldDistrictList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		BaseActivity.POS = 0;
		setContentView(R.layout.activity_list);
		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();

		String tag = bundle.getString("tag");
		OldDistrictManager odManager = new OldDistrictManager(
				getApplicationContext());
		if (tag.equals("search")) {
			oldDistrictList = (ArrayList<OldDistrict>) intent
					.getSerializableExtra("list");
		} else {
			String id = bundle.getString("id");
			oldDistrictList = odManager.searchByOldProvince(id);
		}
		bitArray = new ArrayList<Bitmap>();

		for (OldDistrict item : oldDistrictList) {
			try {
				bitArray.add(getBitmapFromAsset("Icons/DiaChi/"
						+ item.getOldDistrictIcon()));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		oldDistrictView = (ListView) findViewById(R.id.list);
		adapter = new OldDistrictListAdapter(getApplicationContext(),
				oldDistrictList, bitArray);
		adapter.notifyDataSetChanged();
		oldDistrictView.setAdapter(adapter);
	}

	private Bitmap getBitmapFromAsset(String strName) throws IOException {
		AssetManager assetManager = getAssets();
		InputStream istr = assetManager.open("img/" + strName);
		Bitmap bitmap = null;
		bitmap = BitmapFactory.decodeStream(istr);
		return bitmap;
	}
}
